# 默认编译器和工具
CC = gcc
LD = gcc
AR = ar
RM = rm -f

# 支持交叉编译，CROSS_COMPILE 可通过命令行指定，例如：make CROSS_COMPILE=arm-none-eabi-
CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)$(CC)
LD := $(CROSS_COMPILE)$(LD)
AR := $(CROSS_COMPILE)$(AR)

# 编译选项
CFLAGS = -Wall -g -O2 -I.
LDFLAGS = -Wall -lprotobuf-c

# 目标可执行文件
TARGET_S = sender 
TARGET_R = receiver

# 源文件和目标文件
SRCS_S = sensor_data.pb-c.c sender.c
OBJS_S = $(SRCS_S:.c=.o)

SRCS_R = sensor_data.pb-c.c receiver.c
OBJS_R = $(SRCS_R:.c=.o)

# 默认目标
.PHONY: all
all: $(TARGET_S) $(TARGET_R)

# 链接目标文件生成可执行文件
$(TARGET_S): $(OBJS_S)
	$(CC) -o $@ $^ $(LDFLAGS)

$(TARGET_R): $(OBJS_R)
	$(CC) -o $@ $^ $(LDFLAGS)

# 编译源文件生成目标文件
%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

# 清理目标
.PHONY: clean
clean:
	$(RM) $(TARGET_S) $(TARGET_R) $(OBJS_S) $(OBJS_R)

# 伪目标依赖
.PHONY: clean all